package de.jmda.cbo.user;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

@NamedQueries
({
	@NamedQuery(
			name=User.QUERY_ALL,
			query="select u from " + User.TABLENAME + " u"),
	@NamedQuery(
			name=User.QUERY_COUNT,
			query="select count(u) from " + User.TABLENAME + " u"),
	@NamedQuery(
			name=User.QUERY_BY_USERNAME,
			query="select u from " + User.TABLENAME + " u where u.username = :username")
})
@Entity
public class User implements Serializable
{
	private static final long serialVersionUID = 1L;

	public final static String TABLENAME = "User";
	public final static String QUERY_ALL = TABLENAME + "All";
	public final static String QUERY_COUNT = TABLENAME + "Count";
	public final static String QUERY_BY_USERNAME = TABLENAME + "ByUsername";

	@Id
	@GeneratedValue
	private long id;

	/**
	 * authentification data: username
	 */
	@Column(unique=true, nullable=false)
	private String username;

	/**
	 * authentification data: password
	 */
	@Column(nullable=false)
	private String password;

	/**
	 * optional email address
	 */
	private String email;

	/**
	 * required by JPA with at least package visibility
	 */
	User()
	{
		super();
	}

	public User(String username, String password, String email)
	{
		this();

		// TODO parameter check
		setUsername(username);
		setPassword(password);
		setEmail(email);
	}

	public User(String username, String password)
	{
		this(username, password, null);
	}

	public long getId()
	{
		return this.id;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = Encryptor.encryptPassword(password);
	}

	public String getEmail()
	{
		return this.email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

  public String asString()
  {
	  return "User [" +
	  		"id=" + id + ", " +
	  		"username="+ username + ", " +
	  		"password="+ password + ", " +
	  		"email=" + email + "]";
  }
}